#include "TcpClient.hpp"

enum
{
    LEFT,
    OPER,
    RIGHT
};

Request ParseLine(const std::string line)
{
    std::string _x, _y;
    char _op;
    int status = LEFT;

    size_t i = 0;
    while (i < line.size())
    {
        switch (status)
        {
        case LEFT:
            if (isdigit(line[i]))
            {
                _x.push_back(line[i++]);
            }
            else
            {
                status = OPER;
            }
            break;
        case OPER:
            _op = line[i++];
            status = RIGHT;
            break;
        case RIGHT:
            if (isdigit(line[i]))
            {
                _y.push_back(line[i++]);
            }
            break;
        }
    }
    Request req;
    req._x = Util::ToInt(_x);
    req._op = _op;
    req._y = Util::ToInt(_y);

    std::cout << req._x << " " << req._op << " " << req._y << std::endl;
    return req;
}

void Usage(const std::string proc)
{
    std::cout << "\n\t" << proc << "clientIp clientPort" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(USER_ERR);
    }

    std::string clientIp = argv[1];
    uint16_t clientPort = atoi(argv[2]);
    ns_client *client = new ns_client(clientIp, clientPort, ParseLine);
    client->InitClient();
    client->start();
    delete client;
    return 0;
}
